package Pattern.ObserverPattern.program3;

import java.util.ArrayList;

public class Test3 {
    public static void main(String[] args) {
        DepartmentEvent departmentEvent = new DepartmentEvent();
        People people = new People("教师：杨锐");
        People people1 = new People("学生：慈胜鑫");
        departmentEvent.attach(people);
        departmentEvent.attach(people1);
        departmentEvent.update("医学院");
    }
}

abstract class Department{
    String Collegename="计算机工程学院";

    public Department() {
    }

    public Department(String name) {
        this.Collegename = name;
    }

    ArrayList<People> people = new ArrayList<People>();

    public void attach(People p){
        people.add(p);
    }

    public void detach(People p){
        people.remove(p);
    }
    public abstract void update(String name);
}

class DepartmentEvent extends Department{

    public DepartmentEvent(String name) {
        super(name);
    }

    public DepartmentEvent() {
    }

    @Override
    public void update(String name) {
        if(name!=super.Collegename){
            for (People obj: people){
                System.out.println("您的院系名称已更改："+"由 "+
                        super.Collegename+" 改为 "+name);
                obj.response();
            }
        }
    }
}

interface DepListener{
    void response();
}

class People implements DepListener{
    private String name;

    public People(String name) {
        this.name = name;
    }

    @Override
    public void response() {
        System.out.println("更改院系师生："+name);
        System.out.println();
    }
}

